﻿using mars_server.csharp.AO;
using mars_server.csharp.AO.impl;
using mars_server.csharp.CO;
using mars_server.csharp.dao;
using mars_server.csharp.dao.impl;
using mars_server.csharp.enums;
using mars_server.csharp.model;
using mars_server.csharp.socket;
using mars_server.csharp.Util;
using mars_server.csharp.VO;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mars_server.csharp.controller
{
    public class VerifyController
    {
        private VerifyController() { }
        private static readonly VerifyController instance = new VerifyController();
        public static VerifyController Instance
        {
            get
            {
                return instance;
            }
        }

        UserAO userAO = UserAOImpl.Instance;

        UserSocketAO userSocketAO = UserSocketAOImpl.Instance;

        UserSocketManager userSocketManager = UserSocketManagerImpl.Instance;



        /**
         * 登录操作控制器
         * */
        public void Login (MessageCO messageCO ,UserSocket userSocket)
        {
            LoginVO loginVO = JsonUtil.j2o<LoginVO>(messageCO.content);
            UserDO userDO = userAO.verifyUserLogin(loginVO);
            MessageCO sMessageCO = new MessageCO();
            sMessageCO.type = (int)RequestEnum.request.Login;
            sMessageCO.content = userDO;
            if(userDO != null && userDO.username != "")
            {
                if (userSocketManager.selectUserSocketByUserId(userDO.id) != null)//存在已登录过的用户
                {
                    userSocketManager.deleteUserSocketByUserId(userDO.id);//删除冗余数据
                    Console.WriteLine("[info]--【删除】用户重复登录，删除老登录用户-----------");
                }
                userSocket.userDO = userDO;//用户登录成功绑定套接字和用户信息
                Console.WriteLine("[info]:|---"+userDO.username+"登录成功");
            }
            userSocketAO.sendMessageToSocket(userSocket, sMessageCO);
        }

    }
}
